package action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections4.map.HashedMap;

import com.google.gson.Gson;

import pojo.BaseDataPojo;
import pojo.BookImage;
import service.AppService;
import service.MallService;
import serviceImpl.AppServiceImpl;
import serviceImpl.MallServiceImpl;


@WebServlet("/AppServlet")
public class AppServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

    public AppServlet() {
        super();

    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String mark = request.getParameter("mark");
		switch(mark) {
		
		case "getUserid":   // 通过token获取userid
			getUserid(request,response);
			break;
		case "getUserMoney":   // 通过userid获取userMoney
			getUserMoney(request,response);
			break;
		case "addUserMoney":   // 通过userid充值userMoney
			addUserMoney(request,response);
			break;
		}
		}
	private void addUserMoney(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		int userid = Integer.parseInt(request.getParameter("userid"));
		int userMoney = Integer.parseInt(request.getParameter("userMoney"));
		int credit = Integer.parseInt(request.getParameter("credit"));
		int totalMoney =userMoney + credit;
		AppService ms = new AppServiceImpl();
		boolean result = ms.addUserMoney(userid,totalMoney);
		Map<String,Boolean> map = new HashedMap<>();
		map.put("result",result);
		System.out.println("result:"+result);
		if(result) {
			out.print(new Gson().toJson(new BaseDataPojo<Map<String, 
					Boolean>>("成功", true, map)));
		}else {
			out.print(new Gson().toJson(new BaseDataPojo<Map<String, 
					Boolean>>("为空或失败", false, map)));
		}
	}
	

	private void getUserMoney(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		int userid = Integer.parseInt(request.getParameter("userid"));
		
		AppService ms = new AppServiceImpl();
		int userMoney;
		userMoney = ms.getUserMoney(userid);
		Map<String,Integer> map = new HashedMap<>();
		map.put("userMoney",userMoney);
		
		//System.out.println("userMoney:"+userMoney);
		if(userMoney!=0) {
			out.print(new Gson().toJson(new BaseDataPojo<Map<String, 
					Integer>>("通过userid获取userMoney成功", true, map)));
		}else {
			out.print(new Gson().toJson(new BaseDataPojo<Map<String, 
					Integer>>("通过userid获取userMoney为空或失败", false, map)));
		}
		
	}

	private void getUserid(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		String token = request.getParameter("token");
		
		AppService ms = new AppServiceImpl();
		int userid;
		userid = ms.getUserid(token);
		Map<String,Integer> map = new HashedMap<>();
		map.put("userid",userid);
		
		//System.out.println("userid:"+userid);
		if(userid!=0) {
			out.print(new Gson().toJson(new BaseDataPojo<Map<String, 
					Integer>>("根据token获取userid成功", true, map)));
		}else {
			out.print(new Gson().toJson(new BaseDataPojo<Map<String, 
					Integer>>("根据token获取userid为空或失败", false, map)));
		}
		
	}

}
